 $(document).ready(function() {
 	///分页
 	function pageDivide(count, curr) {

 		layui.use(['laypage', 'layer'], function() {
 			var laypage = layui.laypage,
 				layer = layui.layer;
 			laypage.render({
 				elem: 'demo7',
 				limit: 15,
 				curr: curr,
 				count: count,
 				layout: ['count', 'prev', 'page', 'next', 'skip'],
 				jump: function(obj, first) {
 					if(!first) {
 						$('.data_cont').html('');
 						adivceListData(obj.curr)
 					}
 				}
 			});
 		})

 	}
 	//初始化表格
 	adivceListData(1);
 	//获取表格数据
 	function adivceListData(curr) {

 		$.ajax({
 			type: "get",
 			url: "https://www.retux.cn/MgAdvice",
 			data: {
 				page: curr,
 				row: 15
 			},
 			success: function(data) {
 				adviceTableView(data[0]);
 				pageDivide(data[1], curr);
 			}
 		});

 	}
 	//表格数据渲染
 	function adviceTableView(data) {

 		for(var i = 0; i < data.length; i++) {

 			var tabsText = `<tr  index="${data[i][3]}">
 	 						<td><input type="checkbox"></input></td>
      						<td>${data[i][0]}</td>
      						<td>${data[i][1]}</td>
      						<td>${data[i][2]}</td>
      						<td><a class="look_item">查看</a></td>
      						<td><a class="had_look">已读</a></td>
      		 				</tr>`;
 			$('.data_cont').append(tabsText);
 		}

 		tableEdit();

 	}

 	//表格操作	
 	function tableEdit() {
        //多选
 		$("#check_all").click(function() {
 			if($(this).prop('checked') == true) {

 				$("tbody").find('input[type=checkbox]').prop('checked', 'checked')
 			} else {
 				$("tbody").find('input[type=checkbox]').prop('checked', false)
 			}
 		})

 		//查看详情
 		$('.look_item').click(function() {

 			var eventid = $(this).parent().parent().attr('index');
 			$.ajax({
 				type: "get",
 				url: "https://www.retux.cn/MgAdviceDetail/",
 				async: true,
 				data: {
 					Id: eventid
 				},
 				success: function(data) {
 					layer.open({
 						type: 1,
 						title: '建议详情',
 						closeBtn: 3,
 						area: ['350px', '200px'],
 						shade: 0.8,
 						shadeClose: true,
 						anim: 1,
 						id: 'LAY_layuipro',
 						btnAlign: 'c',
 						moveType: 1,
 						content: '<div style="padding:20px;">' + data + '</div>',
 						success: function() {

 						}

 					})
 				}
 			});

 		})
 		//已读操作
 		$('.had_look').click(function() {

 			var checkNode = $("tbody").find('input[type=checkbox]:checked');
 			var checkAll = []; //保存多选的所有id
 			var eventid = $(this).parent().parent().attr('index');

 			if(checkNode.length > 0) {
 				for(var i = 0; i < checkNode.length; i++) {
 					var eventId = checkNode.eq(i).attr('index');
 					checkAll.push(eventId);
 				}
 			} else {
 				checkAll.push(eventid);
 			}

 			$.ajax({
 				type: "get",
 				url: "https://www.retux.cn/MgAdviceDel/",
 				async: true,
 				data: {
 					Id: checkAll.join('-')
 				},
 				success: function(data) {
 					if(data == 'success') {
 						layer.msg('success', {
 							time: 500
 						}, function() {
 							location.reload()
 						})
 					} else {
 						layer.msg('failure', {
 							time: 500
 						})
 					}
 				}
 			});
 		})
 	}
 })